<template>
	<view>
		<!-- 自定义搜索组件 -->
		<my-search @click="gotoSearch"></my-search>
		<!-- uni-app 导航条带搜索组件 -->
		<!-- <uni-nav-bar backgroundColor="#C00000">
			<view class="input-view">
				<uni-icons class="input-uni-icon" type="search" size="18" />
				<input confirm-type="search" class="nav-bar-input" type="text" placeholder="输入搜索关键词"
					@confirm="searchConfirm"/>
			</view>
		</uni-nav-bar> -->
		
		
		<view class="scroll-view-container">
			<!-- 左侧的滚动视图区域 -->
			<scroll-view class="left-scroll-view" scroll-y :style="{height: originData.wh + 'px'}">
				<block v-for="(item, i) in originData.cateList" :key="i">
					<view :class="['left-scroll-view-item', i === originData.active ? ' active' : '']" @click="activeChanged(i)">{{item.cat_name}}</view>
				</block>
			</scroll-view>
			<!-- 右侧的滚动视图区域 -->
			<scroll-view class="right-scroll-view" scroll-y :style="{height: originData.wh + 'px'}" :scroll-top="originData.scrollTop">
				<view class="cate-lv2" v-for="(item2, i2) in originData.cateLevel2" :key="i2">
					<view class="cate-lv2-title">/ {{item2.cat_name}} /</view>
					<!-- 动态渲染三级分类的列表数据 -->
					<view class="cate-lv3-list">
						<!-- 三级分类 item 项 -->
						<view class="cate-lv3-item" v-for="(item3, i3) in item2.children" :key="i3" @click="gotoGoodList(item3)">
							<!-- 图片 -->
							<image :src="item3.cat_icon"></image>
							<!-- 文本 -->
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { ref, reactive } from 'vue'
	import { onLoad, onShow } from "@dcloudio/uni-app"
	import badgeMix from '../../../mixins/tabbar-badge.js'
	
	mixins:[badgeMix]
	// 综合定义方案
	const originData = reactive({
		// 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
		wh: 0,
		cateList: [],
		// 当前选中项的索引，默认让第一项选中
		active: 0,
		// 二级分类列表
		cateLevel2: [],
		// 滚动条距离顶部的距离
		scrollTop: 0
	});
	onLoad((option) => {
		// 获取当前系统的信息
		const sysInfo = uni.getSystemInfoSync();
		// 为 wh 窗口可用高度动态赋值
		originData.wh = sysInfo.windowHeight - 50;
		
		getCateList();
	})
	
	// 获取分类列表数据
	const getCateList = async ()=> {
		const { data: res } = await uni.$http.get(
		// uni.$http.baseURL + '/categories'
		'https://api-hmugo-web.itheima.net/api/public/v1/categories'
		);
		if(res.meta.status !== 200) return uni.$showMsg();
		
		originData.cateList = res.message;
		// 为二级分类赋值
		originData.cateLevel2 = res.message[0].children;
	}
	
	// 选中项改变的事件处理函数
	const activeChanged = (i)=> {
		originData.active = i;
		// 为二级分类列表重新赋值
		originData.cateLevel2 = originData.cateList[i].children;
		// 在 0 和 1 之间切换
		originData.scrollTop = originData.scrollTop ? 0 : 1;
	}
	
	// 点击三级分类项跳转到商品列表页面
	const gotoGoodList = (item3)=> {
		uni.navigateTo({
			url:'/subpkg/goods_list/goods_list?cid=' + item3.cat_id
		});
	}
	
	// 搜索确认
	const searchConfirm = ()=> {
		uni.showToast({
			title:'搜索'
		})
	}
	// 自定义搜索框点击跳转
	const gotoSearch = ()=> {
		uni.navigateTo({
			url:'/subpkg/search/search'
		})
	}
	
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;
		
		.left-scroll-view {
			width: 120px;
			
			.left-scroll-view-item {
				line-height: 60px;
				background-color: #F7F7F7;
				text-align: center;
				font-size: 12px;
				
				// 激活项的样式
				&.active {
					background-color: #FFFFFF;
					position: relative;
					font-weight: bold;
					// 渲染激活项左侧的红色指示边线
					&::before {
						content: ' ';
						display: block;
						width: 3px;
						height: 20px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}
			}
		}
		.right-scroll-view {
			.cate-lv2-title {
				font-size: 12px;
				font-weight: bold;
				text-align: center;
				padding: 15px 0;
			}
		}
		
		.cate-lv3-list {
			display: flex;
			flex-wrap: wrap;
			
			.cate-lv3-item {
				width: 33.33%;
				margin-bottom: 10px;
				display: flex;
				flex-direction: column;
				align-items: center;
			}
			
			image {
				width: 60px;
				height: 60px;
			}
			
			text {
				font-size: 12px;
			}
		}
	}

	$nav-height: 30px;
	
	.input-view {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		// width: 500rpx;
		flex: 1;
		background-color: #f8f8f8;
		height: $nav-height;
		border-radius: 15px;
		padding: 0 15px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height: $nav-height;
	}
	.input-uni-icon {
		line-height: $nav-height;
	}
	
	.nav-bar-input {
		height: $nav-height;
		line-height: $nav-height;
		/* #ifdef APP-PLUS-NVUE */
		width: 370rpx;
		/* #endif */
		padding: 0 5px;
		font-size: 12px;
		background-color: #f8f8f8;
	}
	
</style>
